﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace _180FrameWork.Common
{ 
    public class AutoUpdateServiceHelper 
    {
        /// <summary>
        /// 版本文件
        /// </summary>
        private static string VersionFileName = "version.ini";
        /// <summary>
        /// 升级文件
        /// </summary>
        private static string UpdateFilename = "up.zip";
        /// <summary>
        /// 返回服务器上程序的版本号 
        /// </summary>
        /// <param name="AppName"></param>
        /// <returns></returns>
        public static string ServerVer(string AppName)
        {
            //先从applications表中获取版本号


            string FilePath = (AppDomain.CurrentDomain.BaseDirectory + "UPDATE\\" + AppName + "\\" + VersionFileName);
            if (!File.Exists(FilePath)) // 文件是否存在
            {
                return "9.9.9.9";                
            }
            else
            {
                return MyFileHelper.Read(FilePath);
            }
        }
        /// <summary>
        /// 服务端文件分块
        /// </summary>
        /// <param name="AppName"></param>
        /// <param name="Offset"></param>
        /// <param name="BufferSize"></param>
        /// <returns></returns> 
        public static byte[] DownloadChunk(string AppName, long Offset, int BufferSize)
        {
            string FilePath = (AppDomain.CurrentDomain.BaseDirectory + "UPDATE\\" + AppName + "\\" + UpdateFilename);
            if (!File.Exists(FilePath)) // 文件是否存在
                //CustomSoapException("文件不存在", "文件 " + FilePath + " 不存在");
                return null;
            long FileSize = new FileInfo(FilePath).Length;
            if (Offset > FileSize) // 要求下载的数据块的偏移大于文件大小，则出错
                //CustomSoapException("错误的下载偏移位置", String.Format("文件大小{0}, 要求下载的位置 {1}", FileSize, Offset));
                return null;
            // 打开文件，读入数据到TmpBuffer[]
            byte[] TmpBuffer;
            int BytesRead;
            using (FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read))
            {
                fs.Seek(Offset, SeekOrigin.Begin);
                TmpBuffer = new byte[BufferSize];
                BytesRead = fs.Read(TmpBuffer, 0, BufferSize);
            }
            if (BytesRead != BufferSize)
            {
                // 最后一块数据可能小于数组大小, 返回实际大小
                byte[] TrimmedBuffer = new byte[BytesRead];
                Array.Copy(TmpBuffer, TrimmedBuffer, BytesRead);
                return TrimmedBuffer;
            }
            else
                return TmpBuffer;
        }

        /// <summary>
        /// 获取服务端文件大小
        /// </summary>
        /// <param name="AppName"></param>
        /// <returns></returns>
        public static long GetFileSize(string AppName)
        {
            string FilePath = (AppDomain.CurrentDomain.BaseDirectory + "UPDATE\\" + AppName + "\\" + UpdateFilename);

            if (!File.Exists(FilePath)) // 文件是否存在
                //CustomSoapException("文件不存在", "文件 " + FilePath + " 不存在");
                return 0;
            else
                return new FileInfo(FilePath).Length;
        }
        /// <summary>
        /// 服务端文件hash值校验
        /// </summary>
        /// <param name="AppName"></param>
        /// <returns></returns>
        public static string CheckFileHash(string AppName)
        {
            string FilePath = (AppDomain.CurrentDomain.BaseDirectory + "UPDATE\\" + AppName + "\\" + UpdateFilename);

            if (!File.Exists(FilePath)) // 文件是否存在
                return "";
            else
                return CheckLocalFileHash(FilePath);
        }

        /// <summary>
        /// 计算文件的哈希值
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static string CheckLocalFileHash(string FilePath)
        {
            SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
            byte[] hash;
            using (FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096))
                hash = sha1.ComputeHash(fs);
            return BitConverter.ToString(hash);
        }
    }
}
